﻿using FrameworkManapro.BusinessModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class RoleDataAccess : BaseDataAccess<Business.Entities.Role>
    {
        public override Business.Entities.Role Get(object id)
        {
            var parameters = new ArrayList() { id };
            var dt = DataAccessComponent.ExecuteSelect("procRoleByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        protected override GMAC.ReportingSystem.Business.Entities.Role ReadRow(System.Data.DataRow row)
        {
            GMAC.ReportingSystem.Business.Entities.Role role = null;
            if (row != null)
            {
                try
                {
                    role = new GMAC.ReportingSystem.Business.Entities.Role();
                    role.RoleID = (int)row["RoleID"];
                    role.Name = row["RoleName"].ToString();
                    role.Description = row["RoleDescription"].ToString();
                    role.Status = (bool)row["RoleStatus"];
                    role.Security = FrameworkManapro.Utilities.Serializer<GMAC.ReportingSystem.Business.Entities.Security>.ReadXml(row["RolePermisssion"].ToString());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return role;

        }

        protected override List<GMAC.ReportingSystem.Business.Entities.Role> ReadTable(System.Data.DataTable table)
        {
            var roles = new GMAC.ReportingSystem.Business.Entities.RoleCollection();
            GMAC.ReportingSystem.Business.Entities.Role role;
            if (table != null && table.Rows.Count > 0)
            {
                try
                {
                    foreach (System.Data.DataRow row in table.Rows)
                    {
                        role = ReadRow(row);
                        if (role != null)
                        {
                            roles.Add(role);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return roles;
        }

        internal GMAC.ReportingSystem.Business.Entities.Role ReadRole(System.Data.DataRow row)
        {
            return ReadRow(row);
        }

        public GMAC.ReportingSystem.Business.Entities.RoleCollection GetList()
        {
            var dt = DataAccessComponent.ExecuteSelect("procRoleBySelect", null);
            return ReadTable(dt) as GMAC.ReportingSystem.Business.Entities.RoleCollection;
        }

        public GMAC.ReportingSystem.Business.Entities.RoleCollection Search(string RoleName)
        {
            var parameters = new ArrayList() { RoleName };
            System.Data.DataTable dt = DataAccessComponent.ExecuteSelect("procRoleByFilterSelect", parameters);
            return ReadTable(dt) as GMAC.ReportingSystem.Business.Entities.RoleCollection;
        }
    }
}
